﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using CQIE.TES.IService;
using CQIE.TES.Model;
using Newtonsoft.Json.Linq;

namespace CQIE.TES.WEB.ADMIN.Controllers.API
{
    public class TeacherAPIController : ApiController
    {
      

        public ITeacherService TeacherService { get; set; }

        /// <summary>
        /// 获取教师信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="search"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/getallteacher")]
        public object GetTeacherInfo(int page,int limit,string search)
        {
            if (search==null)
            {
                search = "";
            }

            int count;
            List<Teacher> list = TeacherService.GetTeachersPage(page, limit, search,out count);


            var data = new
            {
                code=0,
                count,
                msg="success",
                data=list.Select(r=>new {
                    id=r.ID,
                    teacherName=r.Name,
                    workNumber=r.WorkNumber,
                    age = DateTime.Now.Year - r.Birthday.Value.Year,
                    phone = r.Phone,
                    sex = r.sex==0?"女":"男",
                    info = r.Info,
                    role = r.RoleName,
                    state = r.IsFreeze
                })
            };

            return data;
        }

        [HttpGet]
        [Route("api/freeze")]
        public bool FreezeUser(int id)
        {

            return TeacherService.FreezeUser(id);
        }

        [HttpPost]
        [Route("api/updatateacher")]
        public void Updata([FromBody]JObject data)
        {
            TeacherService.updata(data);
        }

        [HttpGet]
        [Route("api/resetpwd")]
        public void ResetPassWord(int id)
        {
            TeacherService.ResetPassWord(id);
        }

        [HttpPost]
        [Route("api/insertteacher")]
        public void InsertTeacher([FromBody] JObject data)
        {
            TeacherService.InsertTeacher(data);
        }

        [HttpGet]
        [Route("api/detail/searchteacher")]
        public object SearchTeacherByName(string name)
        {
            var data = TeacherService.SearchByName(name);
            var res = data.Select(r => new
            {
                name = r.Name,
                id = r.ID
            });
            return res;
        }
    }
}
